package cn.iocoder.yudao.module.esim.controller.admin.esproduct;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.esim.controller.admin.esproduct.vo.*;
import cn.iocoder.yudao.module.esim.dal.dataobject.esproduct.EsProductDO;
import cn.iocoder.yudao.module.esim.service.esproduct.EsProductService;

@Tag(name = "管理后台 - eSim商品")
@RestController
@RequestMapping("/esim/es-product")
@Validated
public class EsProductController {

    @Resource
    private EsProductService esProductService;

    @PostMapping("/create")
    @Operation(summary = "创建eSim商品")
    @PreAuthorize("@ss.hasPermission('esim:es-product:create')")
    public CommonResult<String> createEsProduct(@Valid @RequestBody EsProductSaveReqVO createReqVO) {
        return success(esProductService.createEsProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新eSim商品")
    @PreAuthorize("@ss.hasPermission('esim:es-product:update')")
    public CommonResult<Boolean> updateEsProduct(@Valid @RequestBody EsProductSaveReqVO updateReqVO) {
        esProductService.updateEsProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除eSim商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('esim:es-product:delete')")
    public CommonResult<Boolean> deleteEsProduct(@RequestParam("id") String id) {
        esProductService.deleteEsProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得eSim商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('esim:es-product:query')")
    public CommonResult<EsProductRespVO> getEsProduct(@RequestParam("id") String id) {
        EsProductDO esProduct = esProductService.getEsProduct(id);
        return success(BeanUtils.toBean(esProduct, EsProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得eSim商品分页")
    @PreAuthorize("@ss.hasPermission('esim:es-product:query')")
    public CommonResult<PageResult<EsProductRespVO>> getEsProductPage(@Valid EsProductPageReqVO pageReqVO) {
        PageResult<EsProductDO> pageResult = esProductService.getEsProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EsProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出eSim商品 Excel")
    @PreAuthorize("@ss.hasPermission('esim:es-product:export')")
    @OperateLog(type = EXPORT)
    public void exportEsProductExcel(@Valid EsProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EsProductDO> list = esProductService.getEsProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "eSim商品.xls", "数据", EsProductRespVO.class,
                        BeanUtils.toBean(list, EsProductRespVO.class));
    }

    @GetMapping("/syn-from-api")
    @Operation(summary = "从api同步产品")
    @PreAuthorize("@ss.hasPermission('esim:es-product:query')")
    public CommonResult<?> synProductFromApi() {
        esProductService.synProductFromApi();
        return success(null);
    }

}